home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 19.zip / BS1 part 19 / AmigaLibDisk 154.adf / DiskLib / tolib.c < prev   
C/C++ Source or Header  |  1987-06-15  |  4KB  |  163 lines

  1. /***************************************************************
  2. *
  3. *            ToLib.c
  4. *
  5. *        Disk Library Documentation System
  6. *        Convert README.list files to LIB system
  7. *
  8. *    By Wilson Snyder        (c) January 1988
  9. *
  10. ****************************************************************
  11. *
  12. *    NOTICE:
  13. *
  14. *    This program was written as a service to the amiga
  15. *    users.  This program may be used by all parties, including
  16. *    sale (see below) as long as the author's name and address
  17. *    remains intact in the program and documentation.
  18. *    Distribution of this program by parties selling it
  19. *    as part of a commercial for-profit package of over $10
  20. *    must contact the address below before such a package is sold.
  21. *
  22. *    I make no warranties over this program, performance,
  23. *    etc.  And any costs incurred by this program, or its
  24. *    actions are placed on the user.
  25. *
  26. *    PLEASE FORWARD INPROVEMENTS MADE TO THIS PROGRAM TO:
  27. *
  28. *    Wilson Snyder, 15 Davis Parkway, South Burlington, VT 05403
  29. *
  30. ***************************************************************/
  31.  
  32. #include "exec/types.h"
  33. #include "stdio.h"
  34.  
  35. #define VERSION    "AMIGA-TOLIB.  Version 1.0.0.  By Wilson Snyder"
  36.  
  37. #define BRK    70    /* Where to break up long, continous lines */
  38. #define BRKTIT    40    /* Where to break title */
  39. #define    SPACEEQ    3    /* Spaces seperating filename and description */
  40.  
  41. void    main(argc, argv)
  42.     int argc;
  43.     char *argv [];
  44.     {
  45.     FILE *InFile,*OutFile;        /* Files for reading and writing */
  46.     unsigned char cntr=0;        /* Insert line break every BRK chars */
  47.     int spaces;            /* Number spaces encountered */
  48.     int mode; /* 0=line begin, 1=space drop, 2=coping, 3=title extract */
  49.     BOOL linelast;            /* Copied text on the last line */
  50.     char ch;            /* Current read character */
  51.  
  52.     /* Print usage information */
  53.     if (argv[1][0]=='?') {
  54.         printf ("%s\nUseage: %s Input-File Output-File [header]\n",
  55.             VERSION, argv[0]);
  56.         exit (0);
  57.         }
  58.  
  59.     /* Open input file */
  60.     if (!(InFile = fopen (argv[1],"r"))) {
  61.         printf ("Bad input file.\n");
  62.         exit (0);
  63.         }
  64.  
  65.     /* Open output file */
  66.     if (!(OutFile = fopen (argv[2],"w"))) {
  67.         printf ("Bad output file.\n");
  68.         fclose (InFile);
  69.         exit (0);
  70.         }
  71.  
  72.  
  73.     mode = 0;
  74.     cntr = 0;
  75.     linelast = FALSE;
  76.  
  77.     /* Begin the copy */
  78.     while ((ch=getc(InFile))!=EOF) {
  79.         /* Newline, reset mode to 0, */
  80.         if (ch=='\n') {
  81.         linelast = (mode==1 || mode==2);
  82.         if (linelast) {
  83.             /* output continuation if text printed on last line */
  84.             putc(' ',OutFile);
  85.             putc('&',OutFile);
  86.             }
  87.         putc (ch,OutFile);
  88.         mode = 0;
  89.         continue;
  90.         }
  91.         
  92.         switch (mode) {
  93.         case 0:    /* Beginning of Line */
  94.             cntr = 0;
  95.             /* If begins with space or tab is a text line */
  96.             if (ch==' ' || ch=='\t') {
  97.                 mode = 1;    /* Skip leading spaces */
  98.                 }
  99.             else    {
  100.                 /* Otherwise is a new title */
  101.                 if (linelast)    putc ('\n',OutFile);
  102.                 if (argc>3)    {
  103.                     /* Print optional disk title */
  104.                     fputs (argv[3],OutFile);
  105.                     putc ('\n',OutFile);
  106.                     }
  107.                 spaces = 0;
  108.                 mode = 3;    /* Copy title */
  109.                 putc(ch,OutFile);
  110.                 }
  111.             break;
  112.  
  113.         case 1:    /* Skipping leading spaces or tabs before text */
  114.             if (ch==' ' || ch=='\t')    break;    /* drop it */
  115.             putc (ch,OutFile);
  116.             cntr = 1;
  117.             mode = 2;    /* now copy remainder */
  118.             break;
  119.  
  120.         case 2:    /* Coping to EOL */
  121.             cntr++;
  122.             if (cntr>BRK) {
  123.                 putc('&',OutFile);
  124.                 putc('\n',OutFile);
  125.                 cntr = 0;
  126.                 }
  127.             putc (ch,OutFile);
  128.             break;
  129.  
  130.         case 3:    /* Copying title until tab or several spaces */
  131.             cntr++;
  132.             /* count spaces, after SPACEEQ start text copy */
  133.             if (ch==' ')
  134.                 spaces++;
  135.             /* End of title, found a tab or line over length */
  136.             if ((spaces>=SPACEEQ) || ch=='\t' || cntr>=BRKTIT) {
  137.                 putc ('\n', OutFile);
  138.                 mode = 1;    /* skip rest */
  139.                 break;
  140.                 }
  141.             if (ch==' ') break;
  142.  
  143.             /* Write title info after stored spaces*/
  144.             while (spaces--)    putc (' ', OutFile);
  145.             spaces = 0;
  146.             if (ch!=' ') putc (ch, OutFile);
  147.             if (cntr>=BRKTIT)    mode = 1;    /* Chr limit */
  148.             break;
  149.  
  150.         default:
  151.             /* Bad state */
  152.             printf ("Internal mode error\n");
  153.             fclose (InFile);
  154.             fclose (OutFile);
  155.             exit(20);
  156.         }
  157.         }
  158.  
  159.     /* All done. */
  160.     fclose (InFile);
  161.     fclose (OutFile);
  162.     }
  163.